﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace LpbPrj.Client.Tools.Converter
{
    public class DateTimeStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            object output = null;

            {
                if (value is DateTime input)
                {
                    if (parameter == null)
                    {
                        output = input.ToString();
                    }
                    else if (parameter.ToString().Equals("date"))// 此处获得是字符串类型
                    {
                        output = input.ToShortDateString();
                    }
                    else
                    {
                        output = input.ToString(parameter.ToString());
                    }
                }
            }

            {
                if (value is TimeSpan input)
                {
                    if (parameter == null) return output;
                    var param = System.Convert.ToString(parameter);
                    if (string.IsNullOrEmpty(param)) return output;

                    if (param.Equals("String"))
                    {
                        output = String.Format("{0:00}:{1:00}:{2:00}", input.Minutes, input.Seconds, input.Milliseconds / 10);
                    }

                    //Slider的
                    if (param.Equals("Double"))
                    {
                        output = input.TotalSeconds;
                    }
                }
            }

            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }


    public class Int2DateTimeStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            string output = null;

            {
                if (parameter == null) return output;
                var param = System.Convert.ToString(parameter);
                if (string.IsNullOrEmpty(param)) return output;

                if (value is int item)
                {
                    //眼底相机 FA 荧光时间展示
                    if (param.Equals("FATimeStamp"))
                    {
                        int mm = item / 60;
                        int ss = item % 60;

                        output = $"{mm.ToString().PadLeft(2, '0')}:{ss.ToString().PadLeft(2, '0')}";
                    }

                    //裂隙灯 录像时间展示
                    if (param.Equals("CaptureAviTime"))
                    {
                        int mm = item / 60; //分
                        int ss = item % 60; //秒

                        output = $"{mm.ToString().PadLeft(2, '0')}:{ss.ToString().PadLeft(2, '0')}";
                    }
                }
            }
            return output;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }

}
